﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReAnimation.MVP.ViewInterfaces;
using ReAnimation.MVP.Models.Interfaces;
using ReAnimation.Helpers.WinForms;
using ReAnimation.Domain;
using ReAnimation.Infrastructure.Services.DomainFactories.Interfaces;

namespace ReAnimation.MVP.Presenters
{
    public class AnimationStepsPresenter: BasePresenter<IAnimationStepsView>
    {
        private IMainModel _mainModel;
        private IAnimationFactory _animationFactory;

        public AnimationStepsPresenter(IMainModel mainModel, IAnimationFactory animationFactory)
        {
            _mainModel = mainModel;
            _animationFactory = animationFactory;
        }

        protected override void InitView()
        {
            View.Add += new EventHandler(View_Add);
            View.Remove += new EventHandler(View_Remove);
            View.Apply += new EventHandler(View_Apply);
            View.AnimationStepSelect += new EventHandler<EventArg<AnimationStep>>(View_AnimationStepSelect);
        }

        void View_AnimationStepSelect(object sender, EventArg<AnimationStep> e)
        {
            if (e.Data != null)
            {
                _mainModel.CurrentAnimationStep = e.Data;

                if (_mainModel.CurrentAnimationStep != null &&
                    _mainModel.CurrentAnimationStep.Molecules.Count() > 0)
                    _mainModel.CurrentMolecule = _mainModel.CurrentAnimationStep.Molecules.First();
                else
                    _mainModel.CurrentMolecule = null;
            }
        }

        void View_Apply(object sender, EventArgs e)
        {
            if (_mainModel.CurrentAnimation == null ||
               _mainModel.CurrentAnimation.AnimationSteps == null ||
               _mainModel.CurrentAnimation.AnimationSteps.Count() == 0 ||
               _mainModel.CurrentAnimationStep == null)
                return;

            float percent;
            float.TryParse(View.Percent,out percent);
            if (CheckAnimationStepsForPercent(percent)) return;

            _mainModel.CurrentAnimationStep.Percent = percent;
        }

        void View_Remove(object sender, EventArgs e)
        {
            if (_mainModel.CurrentAnimationStep == null || _mainModel.CurrentAnimation == null) return;
            _mainModel.CurrentAnimation.RemoveAnimationStep(_mainModel.CurrentAnimationStep);

            if (_mainModel.CurrentAnimation.AnimationSteps.Count() == 0)
                _mainModel.CurrentAnimationStep = null;
            else
                _mainModel.CurrentAnimationStep = _mainModel.CurrentAnimation.AnimationSteps.First();
        }

        void View_Add(object sender, EventArgs e)
        {
            var animationStep = _animationFactory.CreateAnimationStep();
            float percent;
            float.TryParse(View.Percent, out percent);
            animationStep.Percent = percent;

            if (CheckAnimationStepsForPercent(percent)) return;

            _mainModel.CurrentAnimation.AddAnimationStep(animationStep);
            _mainModel.CurrentAnimationStep = animationStep;
        }

        private bool CheckAnimationStepsForPercent(float percent)
        {
            foreach (AnimationStep animationStep in _mainModel.CurrentAnimation.AnimationSteps)
            {
                if (animationStep.Percent == percent)
                    return true;
            }
            return false;
        }
    }
}
